from datetime import datetime
from factory import db

class Base(db.Model):
    __abstract__ = True  # 让这个类不会直接映射为表
    id = db.Column(db.Integer, primary_key=True, autoincrement=True)

class CidMixin:
    cid = db.Column(db.Integer)

class PidMixin:
    pid = db.Column(db.Integer)

class ActorMixin:
    uid = db.Column(db.Integer, nullable=False)
    time = db.Column(db.DateTime, nullable=False, default=datetime.now)

class AcceptorMixin:
    uid1 = db.Column(db.Integer)

class TitleMixin:
    title = db.Column(db.String(300))

class TextMixin:
    text = db.Column(db.Text)

class StatusMixin:
    published = db.Column(db.Boolean, default=False)
    checked = db.Column(db.Boolean, default=False)
    passed = db.Column(db.Boolean, default=False)

class SortMixin:
    sort = db.Column(db.Integer)

class AccountMixin:
    username = db.Column(db.String(300), nullable=False, unique=True)
    password = db.Column(db.String(300), nullable=False)

class UrlMixin:
    url = db.Column(db.String(300))

class ImageMixin:
    image = db.Column(db.LargeBinary)

######
def get_by_id(orm,id):
    return orm.query.get(id)

def get_user_by_name(username):
    return User.query.filter_by(username=username).first()

def insert(obj):
    db.session.add(obj)
    db.session.commit()

def delete(obj):
    db.session.delete(obj)
    db.session.commit()

#####

class LikePost(Base, ActorMixin, PidMixin):
    pass

class LikeComment(Base, ActorMixin, CidMixin):
    pass

class LikeUser(Base, ActorMixin, AcceptorMixin):
    pass

class Comment(Base, ActorMixin, PidMixin, TextMixin, StatusMixin):
    pass

class Post(Base, ActorMixin, TitleMixin, TextMixin, StatusMixin, SortMixin):
    pass

class Prove(Base, ActorMixin, StatusMixin, SortMixin):
    pass

class Intro(Base, ActorMixin, TitleMixin):
    pass

class User(Base, AccountMixin):
    pass

class Img(Base,ActorMixin,UrlMixin,ImageMixin,StatusMixin):
    pass




